package com.example.webproject.Controller;

import com.example.webproject.entity.Course;
import com.example.webproject.entity.User;
import com.example.webproject.exception.NotFoundException;
import com.example.webproject.repository.CourseRepository;
import com.example.webproject.repository.UserRepository;
import com.example.webproject.service.CourseService;
import com.example.webproject.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hhs
 * @Description
 * @date 2022/12/12 15:50
 */
@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private UserService userService;

    private CourseRepository courseRepository;

    @GetMapping("/course")
    public ResponseEntity<List<Course>> getCourse(){
        List<Course> courses =courseService.listCourse();
        return new ResponseEntity<>(courses, HttpStatus.OK);
    }
    @GetMapping("/course/{id}")
    public ResponseEntity<Course> getCourseById(@PathVariable("id") long id){
        Course course=courseService.getCourse(id);
        return new ResponseEntity(course, HttpStatus.OK);
    }
    @PostMapping("/user/{id}/course")
    public ResponseEntity<List<User>> saveCourse(@PathVariable("id") long id,@RequestBody Course course){
        User users = userService.getUser(id);
        course.setTeacher(users);
        courseService.saveCourse(course);
        return new ResponseEntity<>(HttpStatus.CREATED);

    }
}
